[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
strdup() Duplicate String
#include <string.h>
char *strdup(string);
const char *string; Source string
strdup() makes a copy of 'string'. It allocates space for the string
with a call to malloc().
Returns: A pointer to the storage location containing the
duplicate string. NULL is returned if space could not
be allocated.
Notes: strdup() expects to operate on null-terminated
strings. No overflow checking is done when strings
are copied or appended.
-------------------------------- Example ---------------------------------
The following statements make a copy of 'jelly beans' and print it
out.
#include <string.h>
#include <stdio.h> /* for printf */
char string[20] = "jelly beans";
char *rslt;
main()
{
if ((rslt = strdup(string)) == NULL)
printf("memory could not be allocated");
else
printf("%s",rslt);
}
See Also:
strcpy()
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson